/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;

import negocio.GerenciarDB;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Uguinho
 */
public class JanelaRecarga extends javax.swing.JFrame {

    /**
     * Creates new form JanelaRecarga
     */
    public JanelaRecarga() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        iRecharge = new javax.swing.JLabel();
        NomedoDepositante = new javax.swing.JLabel();
        NomeDepositante = new javax.swing.JTextField();
        NumdoCartao = new javax.swing.JLabel();
        NumCartaoRecarga = new javax.swing.JFormattedTextField();
        Valor = new javax.swing.JLabel();
        ValorRecarga = new javax.swing.JTextField();
        BotaoConfirmarRecarga = new javax.swing.JButton();
        ErroCampoVazio = new javax.swing.JLabel();
        CancelarRecarga = new javax.swing.JButton();
        LimparRecarga = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("iCards - iRecharge");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        iRecharge.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        iRecharge.setText("iRecharge");
        getContentPane().add(iRecharge, new org.netbeans.lib.awtextra.AbsoluteConstraints(269, 11, -1, -1));

        NomedoDepositante.setText("Nome do depositante:");
        getContentPane().add(NomedoDepositante, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 90, 130, -1));

        NomeDepositante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NomeDepositanteActionPerformed(evt);
            }
        });
        getContentPane().add(NomeDepositante, new org.netbeans.lib.awtextra.AbsoluteConstraints(261, 85, 368, 30));

        NumdoCartao.setText("Nº do cartão a ser creditado:");
        getContentPane().add(NumdoCartao, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 140, 160, -1));

        NumCartaoRecarga.setToolTipText("Digite o número do cartão para recarga");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("####-####");  
            NumCartaoRecarga = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        getContentPane().add(NumCartaoRecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 130, 130, 30));

        Valor.setText("Valor:");
        getContentPane().add(Valor, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 190, 50, -1));

        ValorRecarga.setToolTipText("Valor para recarga");
        getContentPane().add(ValorRecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 70, 30));

        BotaoConfirmarRecarga.setText("Confirmar");
        BotaoConfirmarRecarga.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoConfirmarRecargaActionPerformed(evt);
            }
        });
        getContentPane().add(BotaoConfirmarRecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 430, 100, 30));

        ErroCampoVazio.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        ErroCampoVazio.setForeground(new java.awt.Color(255, 51, 51));
        getContentPane().add(ErroCampoVazio, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 190, -1, -1));

        CancelarRecarga.setText("Cancelar");
        CancelarRecarga.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarRecargaActionPerformed(evt);
            }
        });
        getContentPane().add(CancelarRecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(600, 430, 100, 30));

        LimparRecarga.setText("Limpar");
        LimparRecarga.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LimparRecargaActionPerformed(evt);
            }
        });
        getContentPane().add(LimparRecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 430, 100, 30));

        setSize(new java.awt.Dimension(728, 514));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoConfirmarRecargaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoConfirmarRecargaActionPerformed
        // TODO add your handling code here:
        GerenciarDB banco = new GerenciarDB();
        String verNumCartao = NumCartaoRecarga.getText();
        verNumCartao = verNumCartao.replaceAll("[-]","");
        String valorRecarga = ValorRecarga.getText();
        String depositante = NomeDepositante.getText();
        String CNPJ = null;
        String data = banco.getData();
        
        GerenciarDB cartao = new GerenciarDB();
        
        boolean CheckedIDcartao = false;
        try {
            CheckedIDcartao = cartao.checkCartaoDB(verNumCartao);
            System.out.println(CheckedIDcartao);
        } catch (Exception ex) {
            Logger.getLogger(JanelaRecarga.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        if (((verNumCartao.isEmpty())||(valorRecarga.isEmpty())||(depositante.isEmpty()))==true){
              ErroCampoVazio.setText("Existem campos vazios.");
              
              if (verNumCartao.isEmpty()==true){
                   //ErroNome.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
             
              }else if (valorRecarga.isEmpty()==true){
                   //CNPJErro.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
              }if(depositante.isEmpty()==true){
                  // TelErro.setText("*");
                  ErroCampoVazio.setText("Existem campos vazios.");
              }
         }
         else{
             try {
                System.out.println("Teste");    
                if (CheckedIDcartao) {
                    
                    cartao.addExtrato(depositante, valorRecarga, data, CNPJ, verNumCartao);
                    double valor = Double.parseDouble(valorRecarga);
                    banco.updateSaldoCartaoDB(verNumCartao,banco.getSaldoCartaoDB(verNumCartao), "MAIS", valor);
                    JanelaInicioUsuario frame = new JanelaInicioUsuario();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    this.dispose();  
                } else {
                    ErroCampoVazio.setText("Número do cartão inválido. Digite novamente."); 
                }
            } catch (Exception ex) {
                Logger.getLogger(JanelaRecarga.class.getName()).log(Level.SEVERE, null, ex);
            }
             
         }

    }//GEN-LAST:event_BotaoConfirmarRecargaActionPerformed

    private void NomeDepositanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NomeDepositanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_NomeDepositanteActionPerformed

    private void CancelarRecargaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarRecargaActionPerformed
        // TODO add your handling code here:
        this.dispose();
        JanelaInicioUsuario frame = new JanelaInicioUsuario();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }//GEN-LAST:event_CancelarRecargaActionPerformed

    private void LimparRecargaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LimparRecargaActionPerformed
        // TODO add your handling code here:
        NumCartaoRecarga.setText("");
        ValorRecarga.setText("");
        NomeDepositante.setText("");
        ErroCampoVazio.setText("");
        
    }//GEN-LAST:event_LimparRecargaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaRecarga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaRecarga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaRecarga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaRecarga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JanelaRecarga().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoConfirmarRecarga;
    private javax.swing.JButton CancelarRecarga;
    private javax.swing.JLabel ErroCampoVazio;
    private javax.swing.JButton LimparRecarga;
    private javax.swing.JTextField NomeDepositante;
    private javax.swing.JLabel NomedoDepositante;
    private javax.swing.JFormattedTextField NumCartaoRecarga;
    private javax.swing.JLabel NumdoCartao;
    private javax.swing.JLabel Valor;
    private javax.swing.JTextField ValorRecarga;
    private javax.swing.JLabel iRecharge;
    // End of variables declaration//GEN-END:variables

}

// Lembrar de fazer a msg de envio realizado!!! 
